impl Solution {
    pub fn minimum_average_difference(nums: Vec<i32>) -> i32 {
        let mut sufSum: Vec<i64> = nums
            .iter()
            .rev()
            .scan(0i64, |acc, &ele| {
                let old = *acc;
                *acc += ele as i64;
                Some(old)
            })
            .collect();
        sufSum.reverse();

        let len = nums.len() as i64;
        sufSum
            .iter()
            .zip(nums.iter().enumerate())
            .fold(
                (0i64, 0i64, i64::MAX),
                |(mut pre_sum, mut min_ind, mut min_val), (suf_sum, (ind, &ele))| {
                    let ind = ind as i64;
                    pre_sum += ele  as i64;
                    let pre_avg = pre_sum / (ind + 1);
                    let suf_avg = if ind == len - 1 {
                        0
                    } else {
                        suf_sum / (len - ind - 1)
                    };
                    let abs = (pre_avg - suf_avg).abs();
                    if abs < min_val {
                        min_ind = ind;
                        min_val = abs;
                    }
                    (pre_sum, min_ind, min_val)
                },
            )
            .1 as i32
    }
}
